package service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import service.interf.IDeviceLog;
import action.login.AdminAction;

import common.impl.exception.ServiceException;

import dao.interf.IDeviceInfoDAO;
import dao.interf.IDeviceLogDAO;
import dao.interf.IMemberDAO;
import dao.pojo.DeviceInfo;
import dao.pojo.DeviceLog;
import dao.pojo.Member;

public class DeviceLogImpl extends BaseDataServiceImpl implements IDeviceLog {

	private IMemberDAO memberDao;
	private IDeviceLogDAO deviceLogDao;
	private IDeviceInfoDAO deviceInfoDao;

	public void setMemberDao(IMemberDAO memberDao) {
		this.memberDao = memberDao;
	}

	public void setDeviceLogDao(IDeviceLogDAO deviceLogDao) {
		this.deviceLogDao = deviceLogDao;
	}

	public void setDeviceInfoDao(IDeviceInfoDAO deviceInfoDao) {
		this.deviceInfoDao = deviceInfoDao;
	}

	public String getList(Long userId, int page, int rows) throws Exception {
		String[] pNames = null;
		Object[] pValues = null;
		Member member = memberDao.findById(userId);
		if (null == member)
			throw new ServiceException("请先登录");
		if (!"admin".equals(member.getUserName())) {
			pNames = new String[] { "deviceBindCode" };
			pValues = new Object[] { member.getUserName() };
		}
		int start = (page - 1) * rows;
		if (start < 0)
			start = 0;
		long total = deviceLogDao.getPropertiesCount(pNames, pValues, null,
				null);
		JSONObject jo = new JSONObject();
		jo.put("total", total);
		List<DeviceLog> list = deviceLogDao.findByPropertiesRule(pNames,
				pValues, null, " order by model.logIndex desc", start, rows);
		JSONArray ja = new JSONArray();
		if (null != list && !list.isEmpty()) {
			for (DeviceLog deviceLog : list) {
				JSONObject jobj = new JSONObject();
				jobj.put("logIndex", deviceLog.getLogIndex());
				jobj.put("deviceBindCode", deviceLog.getDeviceBindCode());
				jobj.put("deviceExecAction", deviceLog.getDeviceExecAction());
				jobj.put("deviceExecTime", deviceLog.getDeviceExecTime());
				jobj.put("deviceUpgradeVersion", deviceLog
						.getDeviceUpgradeVersion());
				jobj.put("deviceExecResult", deviceLog.getDeviceExecResult());
				// deviceSn
				String deviceSn = deviceLog.getDeviceSn();
				List<DeviceInfo> list2 = deviceInfoDao.findByProperties(
						new String[] { "deviceSn" }, new Object[] { deviceSn });
				if (null != list2 && !list2.isEmpty()) {
					String deviceName = list2.get(0).getDeviceName();
					if (!StringUtils.isEmpty(deviceName)
							&& !"NONE".equals(deviceName)) {
						deviceSn = deviceName;
					} else {
						String fwsn = AdminAction.snMap.get(deviceSn);
						if (!StringUtils.isEmpty(fwsn))
							deviceSn = fwsn;
					}
				}
				jobj.put("deviceSn", deviceSn);
				ja.add(jobj);
			}
		}
		jo.put("rows", ja);
		return jo.toString();
	}

	public String add(String deviceSn, String deviceBindCode,
			String deviceExecAction, String deviceUpgradeVersion)
			throws Exception {
		DeviceLog deviceLog = new DeviceLog();
		deviceLog.setDeviceSn(deviceSn);
		deviceLog.setDeviceBindCode(deviceBindCode);
		deviceLog.setDeviceExecAction(deviceExecAction);
		deviceLog
				.setDeviceExecTime(myDateHandle
						.MyStringToISODateTimeString(myDateHandle
								.getCurrentDTString()));
		deviceLog.setDeviceUpgradeVersion(deviceUpgradeVersion);
		deviceLog.setDeviceExecResult((short) 7);
		deviceLogDao.save(deviceLog);
		return "1";
	}

}
